<template>
  <div class="chess">
    <div class="page4Nav" v-if="chessTypeList.length > 0">
      <ul>
        <li
          v-for="(item, index) of chessTypeList"
          :key="index"
          :class="activeIndex === index ? 'active' : ''"
          @click="changeChessType(item.id, index)"
        >
          <div class="icon">
            <img :src="item.imgUrl" alt="" />
          </div>
          <p>{{ item.name }}</p>
        </li>
      </ul>
    </div>
    <div class="chessGames">
      <div class="wrap">
        <div class="changeTabRight">
          <ul class="game_list" v-if="list.length > 0">
            <li v-for="item in list" :key="item.id">
              <div class="normal">
                <div class="lazyload-wrapper">
                  <img :src="item.icon" alt="game_icon" class="game_icon" />
                </div>
                <div class="enter" @click="goGame(item.id)">
                  <img
                    src="@/assets/img/games/enter.png"
                    alt="enter-game"
                  />进入游戏
                </div>
              </div>
              <p class="allGame">{{ item.name }}</p>
            </li>
          </ul>
          <p v-else class="noData">{{ $t("noData") }}</p>
        </div>
      </div>
    </div>
    <Loading v-if="listLoading" />
    <LoginDialog
      v-if="isShow"
      @close="
        () => {
          isShow = false;
        }
      "
    />
  </div>
</template>
<script>
import { httpHomeList, httpEnterGame } from "@/http/api";
import Loading from "@/components/loading.vue";
import LoginDialog from "@/components/loginDialog.vue";
export default {
  name: "Chess",
  components: { Loading, LoginDialog },
  props: ["dataList"],
  data() {
    return {
      game_name: "",
      activeTab: "all", // 默认选中的标签
      listLoading: false,
      list: [],
      startAjax: false,
      chessTypeList: [
        {
          imgUrl: require("../../../assets/img/games/开元棋牌.png"),
          name: "开元棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/LG_乐游棋牌.png"),
          name: "乐游棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/天美棋牌.png"),
          name: "美天棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/天豪棋牌.png"),
          name: "天豪棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/百盛棋牌.png"),
          name: "百胜棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/vg棋牌.png"),
          name: "VG棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/FG棋牌.png"),
          name: "FG棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/新世界棋牌.png"),
          name: "新世界棋牌",
        },
        {
          imgUrl: require("@/assets/img/games/双赢棋牌.png"),
          name: "双赢棋牌",
        },
      ],
      activeIndex: 0,
      isShow: false,
      currentId: "",
    };
  },
  methods: {
    async goGame(id) {
      if (!localStorage.getItem("TOKEN")) {
        this.isShow = true;
        return;
      }
      if (this.startAjax) return;
      let params = {
        id,
      };
      this.startAjax = true;
      let res = await httpEnterGame(params);
      if (res.code == 0) {
        window.location.href = res.data.parameter.url;
      } else {
        this.$message.error(res.message);
      }
      this.startAjax = false;
    },
    changeChessType(id, index) {
      this.activeIndex = index;
      this.currentId = id;
      this.getHttpHomeList();
    },

    // 游戏列表
    async getHttpHomeList() {
      let params = {
        page: 1,
        pagesize: 50,
        category_id: this.currentId
          ? this.currentId
          : this.$route.params.id
          ? this.$route.params.id
          : localStorage.getItem("gameTypeId"),
      };
      this.listLoading = true;
      let res = await httpHomeList(params);
      if (res.code == 0) {
        this.list = res.data.list || [];
      }
      this.listLoading = false;
    },
  },
  mounted() {
    this.getHttpHomeList();
    this.chessTypeList =
      this.dataList.filter((item) => {
        return item.id !== 0;
      }) || [];
    this.currentId =
      this.chessTypeList.length > 0 ? this.chessTypeList[0].id : "";
  },
};
</script>
<style lang="less" scoped>
.chess {
  width: 100%;
  min-height: 900px;
  padding: 275px 0 150px;
  background: url("../../../assets/img/games/WechatIMG41.jpg") no-repeat;
  background-size: 100% auto;
  background-position-y: -3px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .page4Nav {
    width: 1200px;
    height: 100px;
    margin: 0 auto;
    overflow: hidden;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 20px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
    ul {
      height: 100%;
      display: flex;
      li {
        cursor: pointer;
        width: 109px;
        height: 80px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .icon {
          width: 100%;
          height: 55px;
          display: flex;
          justify-content: center;
          img {
            width: 50px;
            height: 50px;
          }
        }
        p {
          text-align: center;
          color: #898989;
          font-size: 12px;
        }
      }
      .active {
        p {
          color: #513316;
          font-weight: 700;
        }
      }
    }
  }
  .chessGames {
    width: 1200px;
    min-height: 300px;
    margin: 0 auto;
    overflow: hidden;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 20px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
    padding: 0 20px 50px;
    .game_list {
      padding: 0 20px 0 0;
      display: grid;
      grid-template-columns: repeat(auto-fill, 15%);
      justify-content: space-between;
      gap: 10px;
      min-height: 398px;
      li {
        display: inline-block;
        width: 184px;
        height: 224px;
        position: relative;
        text-align: center;
        margin: 5px;
        border-radius: 5px;
        cursor: pointer;
        overflow: hidden;
        .enter {
          display: none;
          position: absolute;
          width: 100%;
          height: 150px;
          font-size: 14px;
          left: 0;
          top: 0;
          text-align: center;
          z-index: 1;
          color: #fff;
        }
        .normal {
          position: absolute;
          height: 85%;
          width: 100%;
          left: 0;
          top: 0;
          padding: 5px;
          display: flex;
          justify-content: center;
          // align-items: center;
          .game_icon {
            width: 140px;
            height: 130px;
          }
          &:hover {
            .enter {
              display: block;
              display: flex;
              flex-direction: column;
              justify-content: center;
              align-items: center;
            }
            :after {
              content: "";
              position: absolute;
              left: 0;
              top: 0;
              width: 100%;
              height: 100%;
              background: rgba(0, 0, 0, 0.5);
            }
          }
        }
        .allGame {
          background-image: linear-gradient(
            -180deg,
            hsla(0, 0%, 100%, 0),
            rgba(0, 0, 0, 0.8) 70%
          );
          position: absolute;
          bottom: 0;
          left: 0;
          height: 40px;
          width: 100%;
          display: flex;
          align-items: center;
          color: #fff;
          font-size: 16px;
          &:before {
            content: "";
            display: block;
            height: 20px;
            width: 4px;
            border-radius: 5px;
            background-color: #ff6000;
            margin: 0 10px 0 15px;
          }
        }
      }
    }
  }
}
</style>
